package ip.command;
/***********************************************************************
 * Module:  UpdateLanguageCommand.java
 * Author:  mojsilo
 * Purpose: Defines the Class UpdateLanguageCommand
 ***********************************************************************/

import ip.dto.LanguageDTO;
import ip.ejb.session.facade.LanguageFacadeLocal;
import ip.ejb.session.facade.LanguageFacadeLocalHome;
import ip.exceptions.ApplicationException;
import ip.exceptions.NoSuchLanguageException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public class UpdateLanguageCommand extends Command
{
	private LanguageDTO languageDTO = null;
	private LanguageFacadeLocal languageFacadeLocal;
	private Logger logger = Logger.getLogger(this.getClass());

	public void init() throws ApplicationException{
		try {
			logger.debug("init()");
			LanguageFacadeLocalHome languageFacadeLocalHome = (LanguageFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.LanguageFacadeLocalHome.class);
			languageFacadeLocal = languageFacadeLocalHome.create();

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();			
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}

	}

	public void execute() throws ApplicationException{
		try {
			logger.debug("execute()");
			languageFacadeLocal.updateLanguage(languageDTO);
		} catch (NoSuchLanguageException e) {			
			e.printStackTrace();
			throw new NoSuchLanguageException(languageDTO);			
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}
	}

	public void dispose() throws ApplicationException{
		try {
			logger.debug("dispose()");
			languageFacadeLocal.remove();

		} catch (EJBException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (RemoveException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}
	}

	public void setLanguageDTO(LanguageDTO languageDTO)
	{
		this.languageDTO = languageDTO;
	}

}